#include "ProjectViewModel.h"

#include "Project/ProjectViewModelSubsystem.h"
#include "Framework/Editor.h"
#include "Project/ProjectModel.h"

static RProjectViewModel* GProjectViewModel=nullptr;

RProjectViewModel *RProjectViewModel::Get()
{
    return GProjectViewModel;
}

bool RProjectViewModel::IsInitialized()
{
    return GProjectViewModel!=nullptr;
}

void RProjectViewModel::Initialize()
{
    GProjectViewModel=this;
    Super::Initialize();

    if(REditor::Get()
        ->GetSubsystem<RProjectModel>()
        ->IsProjectOpened())
    {
        NotifyProjectOpened();
    }
    

}

void RProjectViewModel::Deinitialize()
{
    GProjectViewModel=nullptr;
    Super::Deinitialize();
}

void RProjectViewModel::FirstTimeInitializeObject()
{
    SubsystemManager=NewObject<RSubsystemManager>(this);

    Super::FirstTimeInitializeObject();
}

void RProjectViewModel::NotifyProjectOpened()
{
    SubsystemManager->InitializeSubsystems(RProjectViewModelSubsystem::StaticClass(),this);
    OnProjectOpened.Broadcast();
}

void RProjectViewModel::NotifyProjectClosed()
{
    OnProjectClosed.Broadcast();
    SubsystemManager->DeinitializeSubsystems();
}
